package com.yearEndWorkload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.common.YearEndExcelName;
import com.yearEndWorkload.listen.ExcelResProp;
import com.yearEndWorkload.listen.WordloadLoadListener;
import com.yearEndWorkload.pojo.entity.WorkloadLoad;
import com.yearEndWorkload.pojo.excel.importTemplate.WorkloadImportExcel;
import com.yearEndWorkload.pojo.form.add.AddWorkloadLoadForm;
import com.yearEndWorkload.pojo.form.update.UpdateWorkloadLoadForm;
import com.yearEndWorkload.pojo.query.TeacherWorkloadQuery;
import com.yearEndWorkload.service.WorkloadLoadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/s3/workload")
@Tag(name = "工作量导入表", description = "工作量导入表相关管理")
//TODO 已有生成的serviceImpl
public class WorkloadController {
    @Resource
    private WorkloadLoadService workloadLoadService;

    @Operation(summary = "①年终工作量表格名称")
    @GetMapping("/getTypeName")
    public Result<?> getTypeName()  {
        List<String> yearEndExcelName = YearEndExcelName.getYearEndExcelName();
        return Result.success(yearEndExcelName);
    }

    @Operation(summary = "②根据年终工作量表格名称,教师获取工作量")
    @PostMapping("/getTeacherWorkload")
    public Result<?> getTeacherWorkload(@RequestBody TeacherWorkloadQuery teacherWorkloadQuery)  {
        return workloadLoadService.getTeacherWorkload(teacherWorkloadQuery);
    }

    @Operation(summary = "添加一个工作量导入")
    @PostMapping("/addWorkloadLoad")
    public Result<?> addWorkloadLoad(@RequestBody AddWorkloadLoadForm addWorkloadLoadForm) {
        return workloadLoadService.addWorkloadLoad(addWorkloadLoadForm);
    }

    @Operation(summary = "批量删除工作量导入")
    @DeleteMapping("/addWorkloadLoad")
    public Result<?> deleteWorkloadLoad(@RequestParam("ids") Integer[] ids) {
        return workloadLoadService.deleteWorkloadLoad(ids);
    }

    @Operation(summary = "查询所有工作量导入")
    @GetMapping("/getWorkloadLoadList")
    public Result<List<WorkloadLoad>> getWorkloadLoadList(@RequestParam("pageNumber") Long pageNumber, @RequestParam("pageSize") Long pageSize) {
        return workloadLoadService.getWorkloadLoadList(pageNumber,pageSize);
    }

    @Operation(summary = "更新工作量导入")
    @PutMapping("/updateWorkloadLoad")
    public Result<?> updateWorkloadLoad(@RequestBody UpdateWorkloadLoadForm updateWorkloadLoadForm) {
        return workloadLoadService.updateWorkloadLoad(updateWorkloadLoadForm);
    }

    @Operation(summary = "批量导入工作量导入")
    @PostMapping("/addByBatch")
    public Result<?> addByBatch(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), WorkloadImportExcel.class,new WordloadLoadListener(workloadLoadService)).sheet().doRead();
        return Result.success(null);
    }

    @Operation(summary = "批量导出工作量导入")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelResProp.setExcelRespProp(response, "工作量导入表");
        List<WorkloadLoad> workloadLoadList = workloadLoadService.list();
        EasyExcel.write(response.getOutputStream())
                .head(WorkloadLoad.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("工作量导入表")
                .doWrite(workloadLoadList);
    }
}
